﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netra
{
    class Galaxy
    {
        public class PlanetDictionary : Dictionary<string, Planet>
        {
            public void Add(Planet planet)
            {
                base.Add(planet.Name,planet);
            }

            private new void Add(string s, Planet p)
            {
                base.Add(s, p);
            }
        }

        static private Random random = new Random();

        //public List<Planet> Planets;
        //public Dictionary<string,Planet> Planets;
        public PlanetDictionary PlanetsByName { get; set; }
        public PlanetDictionary.ValueCollection Planets
        {
            get
            {
                return PlanetsByName.Values;
            }
        }

        public void AddPlanet(Planet planet)
        {
            PlanetsByName.Add(planet.Name, planet);
        }

        public List<Fleet> Fleets;

        public Galaxy()
        {
            PlanetsByName = new PlanetDictionary();
            Fleets = new List<Fleet>();
        }

        private void GeneratePlanet(Planet planet)
        {
            List<Merch> Produced=new List<Merch>();
            List<Merch> Demanded=new List<Merch>();

            int prodno=random.Next(0,3);
            for(int i =0;i<prodno;i++)
                Produced.Add(Merch.TossWareToProduce());
            Demanded.Add(Merch.TossWareToDemand());

            foreach(Merch ware in Produced)
            {
                planet.store.AddProduction(ware,ware.SellPrice,ware.BaseQuantity*random.Next(3,7));
            }

            foreach(Merch ware in Demanded)
            {
                planet.store.AddDemand(ware,ware.BaseQuantity*random.Next(1,3),ware.BuyPrice);
            }

            foreach (Merch ware in Merch.Goods)
            {
                if (!planet.store.Trades(ware))
                    planet.store.AddDemand(ware, ware.BaseQuantity * random.Next(1, 3), ware.BuyPrice / 3 + 2 * ware.SellPrice / 3);
            }
        }

        private void CreateCapital(Planet planet)
        {
            foreach (Merch ware in Merch.Goods)
            {
                switch (ware.Type)
                {
                    case Merch.EType.Raw:
                        planet.store.AddDemand(ware, 3*ware.BaseQuantity, ware.BuyPrice);
                        break;
                    case Merch.EType.Processed:
                        planet.store.AddProduction(ware, ware.SellPrice, 7*ware.BaseQuantity);
                        break;
                }
            }
        }

        public void Generate(int size)
        {
            Planet capital = new Planet(0.5,0.5,"Earth");
            PlanetsByName.Add(capital);
            CreateCapital(capital);

            for(int i=0;i<size;i++)
            {
                double x = random.NextDouble();
                double y = random.NextDouble();
                Planet planet=new Planet(x,y,"P-"+i.ToString());
                PlanetsByName.Add(planet);
                GeneratePlanet(planet);

            }
        }

        public Planet GetAnyPlanet()
        {
            foreach (Planet planet in Planets)
            {
                return planet;
            }
            throw new Exception("Galaxy has no planets");
        }

        public void NextTurn()
        {
            foreach (Planet planeta in Planets)
            {
                planeta.NextTurn();
            }

            foreach (Fleet flota in Fleets)
            {
                flota.Move();
            }

            TradeMgmt.Deals.Commit();
        }

        public override string  ToString()
        {
            string s="";
            foreach (Planet pl in Planets)
                s+=pl.ToString();
            return s;

        }
    }
}
